// pages/product/component/coupon.js
import {
	create,couponList
  } from '../../api/coupon'
  const util = require('../../utils/util.js')
  const app = getApp();
  Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
	  info: {
		type: Object,
		value:{}
	  },

	  shopInfo:{
		type: Object,
		value:{} 
	  },

	  couponList:{
		type: Array,
		value:[]
	  },
	},
	lifetimes: {
	  attached() {
		var _this = this
		_this.getcouponList()
		console.log("************************************************")
		console.log(_this.data.shopInfo)
		console.log(_this.data.couponList)
		console.log("************************************************")
		// _this.data.info.coupons.forEach(function(item) {
		//   item.endDate = util.datetimeFormat(item.endDate)
		// })
		// _this.setData({
		//   info: _this.data.info
		// })
	  }
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		coupons:[]
	},
  
	/**
	 * 组件的方法列表
	 */
	methods: {
		//查询平台优惠券列表
		getcouponList(){
			let _this = this
			let params = {
				mchId:app.globalData.mchId,
				isZero:1,
				sortField:'amount'
			}
			couponList(params).then(
				(res)=>{
					console.info("优惠券信息------")
					console.info(res)
					if (res.type == 'success') {
						if(res.data.data.length > 0){
							if(res.data.data.length > 0){
								this.initCoupons(res.data.data)
							}
						}	
					}
				}
			).catch( )
		},

		initCoupons(data){
			let _this = this
			let coupons = []
			data.map(item => {
				// if(item.stock>0){
				// 	if(item.stock - item.canStock < item.maxgiven){
				// 		item.canReceive = true
				// 	}else{
				// 		item.canReceive = false
				// 	}	
				// }
				item.endDate = util.datetimeFormat(item.endDate)
			})
			_this.setData({
				coupons :data 
			})
			console.info("this.data.coupons***********************")
			console.info(this.data.coupons)
		},
		
	  maskClick(e) {
		  let _this = this
		if (app.globalData.token == null) {
		  wx.navigateTo({
			url: '/pages/login/index',
		  })
		} else {
		  create({
			couponId: e.currentTarget.dataset.id,
			mchId: app.globalData.mchId
		  }).then(res => {
			wx.showToast({
			  title: '领取成功',
			  icon: 'success',
			  duration: 2000
			})
			_this.getcouponList()
		  })
		}
	  },

	  toEnterpriseInfo(){
		wx.navigateTo({
			url: '/pages/shop/enterprise/enterpriseInfo?id='+ this.data.shopInfo.id,
			// url: '/pages/shop/enterprise/enterpriseInfo?id=16',
		  })
	  },

	  cancleClick() {
		this.triggerEvent('cancleClick')
	  }
	}
  })